package com.zzyl.nursing.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.vo.DeviceInfo;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间业务层处理
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //查询房间
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    //新增房间
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    //修改房间
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    //批量删除房间
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    //根据房间ID查询楼层、房间、价格
    @Override
    public RoomVo getRoomById(Long id) {
        return roomMapper.getRoomById(id);
    }

    //根据楼层ID获取房间VO列表
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }

    //获取所有房间（负责老人）
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    //查询智能楼层的基础数据和设备上报的数据
    @Override
    public List<RoomVo> getRoomsWithDeviceByFloorId(Long floorId) {
        //SQL返回的数据是基础数据，找到的是房间、床位、设备（房间|床位）
        List<RoomVo> roomVos = roomMapper.getRoomsWithDeviceByFloorId(floorId);
        roomVos.forEach(roomVo -> {
            //遍历的是房间数据
            List<DeviceInfo> deviceVos = roomVo.getDeviceVos();
            //房间设备所对应的设备上报的数据
            deviceVos.forEach(deviceInfo -> {
                //从Redis中获取指定物联网设备上报的数据
                String jsonStr = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, deviceInfo.getIotId());
                //如果没有数据，跳出本次循环
                if (StringUtils.isEmpty(jsonStr)) {
                    return;
                }
                //将设备上报的数据放入设备数据中
                deviceInfo.setDeviceDataVos(JSONUtil.toList(jsonStr, DeviceData.class));
            });
            //遍历的是床位数据
            roomVo.getBedVoList().forEach(bedVo -> {
                //获取床位对应的设备列表
                bedVo.getDeviceVos().forEach(deviceInfo -> {
                    //从Redis中获取指定物联网设备上报的数据
                    String jsonStr = (String) redisTemplate.opsForHash().get(CacheConstants.IOT_DEVICE_LAST_DATA, deviceInfo.getIotId());
                    //如果没有数据，跳出本次循环
                    if (StringUtils.isEmpty(jsonStr)) {
                        return;
                    }
                    //将设备上报的数据放入设备数据中
                    deviceInfo.setDeviceDataVos(JSONUtil.toList(jsonStr, DeviceData.class));
                });
            });
        });
        return roomVos;
    }
}